/**
 * Created by liubin on 2017/4/27.
 */


var DzpkCardHelper = function () {

    var that = {};

    //牌墙位置
    that.cardWellPoint = null;

    //手牌位置
    that.cardInhandPoints = null;

    //玩家位置
    that.playerPoints = null;

    //筹码位置
    that.chipPoints = null;

    //筹码池位置
    that.chipPotPoints = null,

    that.init = function () {
        that.cardInhandPoints = [];
        that.chipPoints = [];
        that.playerPoints = [];
        that.chipPotPoints = [];
    },

    that.getExistCardNum = function (cardNum) {
        if (cardNum >= 1 && cardNum <= 54){
            return true;
        }
        return false;
    };

    //返回表情序列帧
    that.getFaceFrames = function (index) {
        var spriteFrames = [];
        for (var i = 0; i <= 60; i++){
            var frame = cc.spriteFrameCache.getSpriteFrame("emoji_" + index + "_" + i + ".png");
            if (frame == null){
                break;
            }
            spriteFrames.push(frame);
        }
        return spriteFrames;
    };

    //设置发牌位置
    that.setWellPoint = function (point) {
        that.cardWellPoint = point;
    };

    that.getWellPoint = function () {
        return that.cardWellPoint;
    };

    that.getInhandCardPos = function (seat) {
        return that.cardInhandPoints[seat];
    };

    that.getPlayerPos = function (seat) {
        return that.playerPoints[seat];
    };

    that.getChipPos = function (seat) {
        return that.chipPoints[seat];
    }

    that.getChipPotPos = function (index) {
        return that.chipPotPoints[index];
    }

    //跟要加的注筹码数得到筹码等级序列
    that.getChipLevelArray = function (chipNum) {
        var levelArray = [];

        var basepoint = mo.dzpkTable.mGameConfig.basepoint;

        //2, 10, 50

        var level_3_num = parseInt(chipNum / (basepoint * 50));
        var level_2_num = parseInt(chipNum % (basepoint * 50) / (basepoint * 10));
        var level_1_num = Math.ceil(chipNum % (basepoint * 50) % (basepoint * 10) / (basepoint * 2));

        if(level_3_num == 0 && level_2_num == 0 && level_1_num == 0){
            level_1_num = 1;
        }

        for (var i=0; i<level_3_num; i++){
            levelArray.push(3);
        }

        for (var i=0; i<level_2_num; i++){
            levelArray.push(2);
        }

        for (var i=0; i<level_1_num; i++){
            levelArray.push(1);
        }

        return levelArray;

    };

    //返回牌型对应的中文
    that.getCardTypeCHS = function(cardType){
        var types = [
            "皇家同花顺",
            "同花顺",
            "四条",
            "葫芦",
            "同花",
            "顺子",
            "三条",
            "两对",
            "对子",
            "高牌",
            ""
        ];

        return types[cardType];
    };


    // RoyalFlushdzpk = 0; 		//皇家同花顺
    // StraighFlushdzpk = 1;		//同花顺
    // FourOfKinddzpk = 2;		//四条
    // FullHousedzpk = 3; 		//葫芦
    // Flushdzpk = 4; 			//同花
    // Straightdzpk = 5; 		//顺子
    // ThreeOfKinddzpk = 6;  		//三条
    // TwoPairdzpk = 7; 		//两对
    // OnePairdzpk = 8; 		//对子
    // NoPairdzpk = 9; 		//高牌
    // Anonymousdzpk = 10;		//匿名  一般情况是弃牌的牌型

    that.getMaxCardArray = function (MaxCards, cardType) {
        var lightCardNum = [];

        if(cardType == 0 || cardType == 1 || cardType == 3 || cardType == 4 || cardType == 5){
            for (var i=0; i<MaxCards.length; i++){
                lightCardNum.push(MaxCards[i]);
            }
            return lightCardNum;
        }

        MaxCards.sort(function (a, b) {
            var abs_a = that.getAbsCardNum(a);
            var abs_b = that.getAbsCardNum(b);
            if(abs_a == 1 || abs_b == 1){
                return that.getAbsCardNum(b)-that.getAbsCardNum(a);
            }
            return  that.getAbsCardNum(a)-that.getAbsCardNum(b);
        })

        if(cardType == 9){
            lightCardNum.push(MaxCards[MaxCards.length - 1]);
            return lightCardNum;
        }else{
            //找出所有组成对子，三条，四条的牌
            for (var i=0; i<MaxCards.length-1; i++){
                if(that.getAbsCardNum(MaxCards[i]) == that.getAbsCardNum(MaxCards[i+1])){

                    var hasAdd = false;
                    for (var j=0; j<lightCardNum.length; j++){
                        if(lightCardNum[j] == MaxCards[i]){
                            hasAdd = true;
                        }
                    }

                    if(hasAdd == false){
                        lightCardNum.push(MaxCards[i])
                    }

                    lightCardNum.push(MaxCards[i+1])
                }
            }
            return lightCardNum;
        }
    }

    that.getAbsCardNum = function (cardNum) {
        return (cardNum-1) % 13 + 1;
    }

    return that;
};

mo.dzpkCardHelper = new DzpkCardHelper();